
package com.platform.common.utils;


import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDate;
import java.util.*;

public class JJRUtil {

    public static String sendGet(String url, String param) throws IOException {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "/" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(0);
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            int cot = 0;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    //static Map totalMap = new TreeMap();

    public static void main(String[] args) {
        Iterator it = null;
        try {
            it = loop("2022", "6").keySet().iterator();
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(it.hasNext()) {
            String temp = (String) it.next();
            System.out.println(temp);
        }

    }



    public static  List<String> loopOfMonth(int year, int month)throws IOException{

        String s = JJRUtil.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");

        s = s.substring(s.indexOf("("));
        s = s.substring(1, s.length() - 2);
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);

        List list = (List) map.get("data");
        if(list == null || list.size() == 0) {
            return null;
        }
        Map data = (Map) list.get(0);
        List<Map> almanac = (List<Map>) data.get("almanac");
        if(almanac == null || almanac.size() == 0) {
            return null;
        }

        List<String> days =new ArrayList<>(almanac.size());
        for(int i = 1; i < almanac.size(); i++) {
            /*所有本月的节假日*/
            String day = ((String)almanac.get(i).get("oDate")).substring(0, 10);
            String status = (String)almanac.get(i - 1).get("status");
            LocalDate date = LocalDate.parse(day);
            if("1".equals(status) && date.getYear()==year && date.getMonthValue()==month) {
                days.add(String.valueOf(date.getDayOfMonth()));
            }
        }
        return days;
    }

    public static TreeMap loop(String year, String month) throws IOException {
        TreeMap totalMap = new TreeMap();
        String s = JJRUtil.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");

        s = s.substring(s.indexOf("("));
        s = s.substring(1, s.length() - 2);
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);

        List list = (List) map.get("data");
        if(list == null || list.size() == 0) {
            return totalMap;
        }
        Map data = (Map) list.get(0);
        List<Map> almanac = (List<Map>) data.get("almanac");
        if(almanac == null || almanac.size() == 0) {
            return totalMap;
        }

        for(int i = 1; i < almanac.size(); i++) {
            //获取日
            String key = ((String)almanac.get(i).get("oDate")).substring(0, 10);
            String status = (String)almanac.get(i - 1).get("status");
            if("1".equals(status)) {
                totalMap.put(key, status);
            }
        }
        return totalMap;

    }
    public static TreeMap setFinal(){
        TreeMap totalMap = new TreeMap();
        totalMap.put("2022-01-01", 1);
        totalMap.put("2022-01-02", 1);
        totalMap.put("2022-01-03", 1);
        totalMap.put("2022-02-01", 1);
        totalMap.put("2022-02-02", 1);
        totalMap.put("2022-02-03", 1);
        totalMap.put("2022-02-04", 1);
        totalMap.put("2022-02-05", 1);
        totalMap.put("2022-02-06", 1);
        totalMap.put("2022-04-03", 1);
        totalMap.put("2022-04-04", 1);
        totalMap.put("2022-04-05", 1);
        totalMap.put("2022-05-01", 1);
        totalMap.put("2022-05-02", 1);
        totalMap.put("2022-05-03", 1);
        totalMap.put("2022-05-04", 1);
        totalMap.put("2022-06-03", 1);
        totalMap.put("2022-06-04", 1);
        totalMap.put("2022-06-05", 1);
        totalMap.put("2022-09-10", 1);
        totalMap.put("2022-09-11", 1);
        totalMap.put("2022-09-12", 1);
        totalMap.put("2022-10-01", 1);
        totalMap.put("2022-10-02", 1);
        totalMap.put("2022-10-03", 1);
        totalMap.put("2022-10-04", 1);
        totalMap.put("2022-10-05", 1);
        totalMap.put("2022-10-06", 1);
        totalMap.put("2022-10-07", 1);
        totalMap.put("2022-12-31", 1);
        totalMap.put("2023-1-1", 1);
        totalMap.put("2023-1-2", 1);
        totalMap.put("2023-1-21", 1);
        totalMap.put("2023-1-22", 1);
        totalMap.put("2023-1-23", 1);
        totalMap.put("2023-1-24", 1);
        totalMap.put("2023-1-25", 1);
        totalMap.put("2023-1-26", 1);
        totalMap.put("2023-1-27", 1);
        totalMap.put("2023-4-5", 1);
        totalMap.put("2023-4-29", 1);
        totalMap.put("2023-4-30", 1);
        totalMap.put("2023-5-1", 1);
        totalMap.put("2023-5-2", 1);
        totalMap.put("2023-5-3", 1);
        totalMap.put("2023-6-22", 1);
        totalMap.put("2023-6-23", 1);
        totalMap.put("2023-6-24", 1);
        totalMap.put("2023-9-29", 1);
        totalMap.put("2023-9-30", 1);
        totalMap.put("2023-10-1", 1);
        totalMap.put("2023-10-2", 1);
        totalMap.put("2023-10-3", 1);
        totalMap.put("2023-10-4", 1);
        totalMap.put("2023-10-5", 1);
        totalMap.put("2023-10-6", 1);

        return totalMap;
    }

    private static Map<String,List<String>> map =new HashMap<>();

    public static List<String> getLocalHoliday(int year, int month){
        try {
            StringBuilder sb =new StringBuilder(year).append(month);
            List<String> list = map.get(sb.toString());
            if (list!=null&&list.size()!=0){
                return list;
            }
            list = loopOfMonth(year, month);
            map.put(sb.toString(),list);
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Map<Integer,String> loop2(String year, String month) throws IOException {
        TreeMap<Integer,String> totalMap = new TreeMap();
        String s = JJRUtil.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");

        s = s.substring(s.indexOf("("));
        s = s.substring(1, s.length() - 2);
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);

        List list = (List) map.get("data");
        if(list == null || list.size() == 0) {
            return totalMap;
        }
        Map data = (Map) list.get(0);
        List<Map> almanac = (List<Map>) data.get("almanac");
        if(almanac == null || almanac.size() == 0) {
            return totalMap;
        }

        for(int i = 1; i < almanac.size(); i++) {
            //获取日
            String key = ((String)almanac.get(i).get("oDate")).substring(8, 10);
            String m = ((String)almanac.get(i).get("oDate")).substring(5, 7);
            String status = (String)almanac.get(i - 1).get("status");
            if("1".equals(status)&&(Integer.parseInt(m)==Integer.parseInt(month))) {
                totalMap.put(Integer.parseInt(key), ((String)almanac.get(i).get("oDate")).substring(0, 10));
            }
        }
        return totalMap;

    }
}
